﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 图书管理系统
{
    public partial class 用户登录 : Form
    {
        public static User user = new User();
        public String flag;
        public 用户登录()
        {
            InitializeComponent();
        }
        public 用户登录(String flag1)
        {
            flag = flag1;
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            string ID = textBox1.Text;
            string password = textBox2.Text;
            string sql = "select * from Readerinfo where ReaderID='" + ID + "' and psw=" + password;
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            try
            {
                if (ID == string.Empty || password == string.Empty)
                {
                    MessageBox.Show("请输入完整信息！", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    conn.Open();
                    DataSet dt = new DataSet();
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
                    sqlDataAdapter.Fill(dt, "Readerinfo");
                    //String name = dt.Tables["Readerinfo"].Rows[0]["ReaderName"].ToString();
                    

                    SqlDataReader sdr = cmd.ExecuteReader();
                    if (sdr.HasRows)
                    {
      
                        //User user = new User();
                        user.Name = dt.Tables["Readerinfo"].Rows[0]["ReaderName"].ToString().Trim();
                        user.Id = dt.Tables["Readerinfo"].Rows[0]["ReaderID"].ToString().Trim();
                        user.Identy = dt.Tables["Readerinfo"].Rows[0]["Identy"].ToString().Trim();
                        //MessageBox.Show("登录成功 "+user.Name+" "+user.Id, "Tips", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.Close();
                        if (flag == "book")
                        {
                            
                            书籍详情 f1 = new 书籍详情();
                            f1.ShowDialog();
                        }
                        else if (flag == "recmdat")
                        {
                            荐购 f2 = new 荐购();
                            f2.ShowDialog();
                        }
                        else if(flag=="info")
                        {
                            首页 f = new 首页(user);
                            f.Show();
                        }
                        else
                        {
                            首页 f = new 首页(user);
                            f.Show();
                        }

                    }
                    else
                    {
                        MessageBox.Show("用户名或密码错误，请重新输入！", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            string ID = textBox3.Text;
            string password = textBox4.Text;
            string sql = "select * from Admtsinfo where AdmtsID='" + ID + "' and AdmtsPsw=" + password;
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            try
            {
                if (ID == string.Empty || password == string.Empty)
                {
                    MessageBox.Show("请输入完整信息！", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    conn.Open();
                    DataSet dt = new DataSet();
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
                    sqlDataAdapter.Fill(dt, "Admtsinfo");
                    SqlDataReader sdr = cmd.ExecuteReader();
                    if (sdr.HasRows)
                    {

                        user.Name = dt.Tables["Admtsinfo"].Rows[0]["AdmtsName"].ToString().Trim();
                        user.Id = dt.Tables["Admtsinfo"].Rows[0]["AdmtsID"].ToString().Trim();

                        管理员界面 f = new 管理员界面();
                        f.Show();
                        this.Close();

                    }
                    else
                    {
                        MessageBox.Show("用户名或密码错误，请重新输入！", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

       

        private void button4_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabControl1.TabPages[2];
        }

        private void button6_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabControl1.TabPages[0];
        }

        private void button5_Click(object sender, EventArgs e)
        {
            SqlConnection conn = new SqlConnection();
            string ID = textBox5.Text;
            string password = textBox8.Text;
            string password1 = textBox9.Text;
            string sql = "select * from Readerinfo where ReaderID='" + ID + "' and psw=" + password;
            SqlCommand cmd = Dao.SqlConnect(sql, conn);
            try
            {
                if (ID == string.Empty || password == string.Empty || password1 == string.Empty || password.Length != password1.Length)
                {
                    MessageBox.Show("请输入完整信息且保证两次密码一致！", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    conn.Open();
                    DataSet dt = new DataSet();
                    SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
                    sqlDataAdapter.Fill(dt, "Readerinfo");
                    SqlDataReader sdr = cmd.ExecuteReader();
                    if (sdr.HasRows)
                    {
                        SqlConnection conn1 = new SqlConnection();
                        string sql1 = "update Readerinfo set psw='" + password1 + "' where ReaderID='" + ID + "'";
                        SqlCommand cmd1 = Dao.SqlConnect(sql1, conn1);
                        conn1.Open();
                        int flag = cmd1.ExecuteNonQuery();
                        conn1.Close();
                        if (flag > 0)
                        {
                            MessageBox.Show("密码重置成功！");
                            tabControl1.SelectedTab = tabControl1.TabPages[0];
                        }
                        }
                    else
                    {
                        MessageBox.Show("用户名或密码错误，请重新输入！", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                conn.Close();
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabControl1.TabPages[2];
        }

        private void 用户登录_Load(object sender, EventArgs e)
        {

        }
    }
}
